import 'package:flutter/material.dart';

import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../../presenter/state_login_presenter.dart';
import '../info_page.dart';
import 'register/register_page.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key, required this.presenter, required this.controller})
      : super(key: key);
  static const routeName = '${InfoPage.routeName}/login';
  final StateLoginPresenter presenter;
  final LoginController controller;

  @override
  LoginPageState createState() {
    return LoginPageState();
  }
}

class LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    widget.controller.clearUser();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(I18n.login),
      ),
      body: Form(
        key: _formKey,
        child: Stack(
          children: [
            Center(
                child: Icon(
              Icons.login,
              color: Colors.grey.shade300,
              size: 240,
            )),
            Column(
              children: [
                TextFormField(
                  keyboardType: TextInputType.phone,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.phone_number,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.phone_regex).hasMatch(value)) {
                      return I18n.invalid_username;
                    }
                    widget.presenter.username = value;
                    return null;
                  },
                ),
                TextFormField(
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.password,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.password_regex).hasMatch(value)) {
                      return I18n.invalid_password;
                    }
                    widget.presenter.password = value;
                    return null;
                  },
                ),
                ElevatedButton(
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      // todo async loading ?
                      widget.controller.userLogin();
                    }
                  },
                  child: const Text(I18n.login),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushNamed(context, RegisterPage.routeName)
                        .then((logged) {
                      if (logged as bool? ?? false) {
                        widget.presenter.onPop();
                      }
                    });
                  },
                  child: const Text(I18n.register),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}
